package cn.otrue.patienthealthmanager.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.adapter.MyFragmentPagerAdpater;
import cn.otrue.patienthealthmanager.adapter.ScrollTabsAdapter;
import cn.otrue.patienthealthmanager.view.ScrollTabView;

/**
 * 首页
 * 
 * @author G
 * 
 */
public class MainFragment extends BaseFragment {

	private static ViewPager vp_main_viewpager;
	private ScrollTabView st_main_tabs;
	private ScrollTabsAdapter tabsAdapter;
	private MyFragmentPagerAdpater adapter;
	private TextView tv_title_other;
	private TextView tv_title_othuu;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_main, container,
				false);
		initView(rootView);
		vp_main_viewpager.setCurrentItem(4);

		return rootView;
	}

	private void initView(View rootView) {

		st_main_tabs = (ScrollTabView) rootView.findViewById(R.id.st_main_tabs);
		vp_main_viewpager = (ViewPager) rootView
				.findViewById(R.id.vp_main_viewpager);
		tv_title_other = (TextView) getActivity().findViewById(
				R.id.tv_title_other);
		tv_title_othuu = (TextView) getActivity().findViewById(
				R.id.tv_title_othuu);
		initViewPager();

	}

	private void initViewPager() {

		adapter = new MyFragmentPagerAdpater(getFragmentManager());
		adapter.addFragment(MainDataPickFragment.newInstance());
		adapter.addFragment(MainHistoryDataFragment.newInstance());
		adapter.addFragment(MainHealthyPlanFragment.newInstance());
		adapter.addFragment(MainDataInfoFragment.newInstance());
		adapter.addFragment(MainMainFragment.newInstance());
		vp_main_viewpager.setAdapter(adapter);

		tabsAdapter = new ScrollTabsAdapter(getActivity(), 4);
		tabsAdapter.add(getString(R.string.data_pick));
		tabsAdapter.add(getString(R.string.history_data));
		tabsAdapter.add(getString(R.string.hsealth_plan));
		tabsAdapter.add(getString(R.string.data_info));
		st_main_tabs.setAdapter(tabsAdapter);
		st_main_tabs.setViewPager(vp_main_viewpager);
		vp_main_viewpager.setOffscreenPageLimit(4);

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
	}

	public static Fragment newInstance() {
		return new MainFragment();
	}

	@Override
	public String setPageName() {
		// TODO Auto-generated method stub
		return null;
	}

	public static void gotoFragment(int i) {
		vp_main_viewpager.setCurrentItem(i);
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);

		if (isVisibleToUser) {

			// 相当于Fragment的onResume

			if (vp_main_viewpager != null) {
				vp_main_viewpager.setCurrentItem(4);

			}
		} else {
			// 相当于Fragment的onPause

			if (tv_title_other != null) {
				tv_title_other.setVisibility(View.GONE);
			}
			if (tv_title_othuu != null) {
				tv_title_othuu.setVisibility(View.GONE);
			}

		}
	}

}
